import Vue from "vue";
import Vuex from "vuex";
import goodsModel from "../model/goodsModel";
import shoppingcartModel from "../model/shoppingcartModel";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    //只放数据的声明
    navFlag: true,
    loginFlag: false,
    //商品
    goodsList: [],
    detailList: [],
    shoppingList: [],
    firelist: [],
  },
  mutations: {
    //只放数据的修改方法
    //导航显隐
    changeNavFlag(state, navFlag) {
      state.navFlag = navFlag;
    },
    //登录显隐
    changeLoginFlag(state) {
      state.loginFlag = localStorage.getItem("username").length;
    },
    //获取数据
    initGoodsList(state, list) {
      state.goodsList = list;
    },
    //获取详情
    initDetailList(state, list) {
      state.detailList = list;
    },
    //购物车渲染
    initShoppingCartList(state, list) {
      state.shoppingList = list;
    },
    //热卖推荐
    initFireList(state, list) {
      state.firelist = list;
    },
  },
  actions: {
    //异步数据
    asyncInitGoodsList(context) {
      goodsModel.query().then((res) => {
        context.commit("initGoodsList", res.data);
      });
    },
    asyncInitDetailList(context) {
      goodsModel.query({ id: 2 }).then((res) => {
        context.commit("initDetailList", res.data);
      });
    },
    //购物车渲染
    asyncInitShoppingCartList(context) {
      shoppingcartModel.query().then((res) => {
        context.commit("initShoppingCartList", res.data);
      });
    },
    //分类列表
  },
  modules: {
    //数据很多可以加入模块
  },
});
